<template>
  <div class="mod-shops">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="pageIndex = 1;getDataList()">
      <el-form-item>
        <el-input v-model="searchForm.name" placeholder="店铺名字" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.type"  clearable placeholder="请选择"
                   class="width185" >
          <el-option
            v-for="tag in [{'id':1,'name':'普通店铺'},{'id':3,'name':'商圈'}]"
            :key="tag.id"
            :label="tag.name"
            :value="tag.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="pageIndex = 1; getDataList()">查询</el-button>
        <el-button v-if="isAuth('mall:shops:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
<!--        <el-button v-if="isAuth('mall:shops:uploadSave')" type="primary" @click="batchHandle()">导入</el-button>-->
        <el-button v-if="isAuth('mall:shops:uploadSave')" type="primary" @click="$refs.dataImport.init('/mall/shops/uploadSave',[{titles:['店铺名称','联系电话','详细位置','经纬度','简介','店铺详情','人均消费金额','店铺评分','商圈名称','品牌名称','营业时间'],fileName:'店铺导入模板',data:[[]]}],null)">导入</el-button>
        <el-button v-if="isAuth('mall:shops:delete')" type="danger" @click="deleteHandle()"
                   :disabled="dataListSelections.length <= 0">批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="名字">
      </el-table-column>
<!--      <el-table-column-->
<!--        prop="imgUrl"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="展示图片">-->
<!--        <template slot-scope="scope">-->
<!--          <img style="height: 30%;width: 30%" v-if="scope.row.imgUrl" @click="openImg(scope.row.imgUrl)"-->
<!--               :src="scope.row.imgUrl"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column
        width="250"
        prop="imgList"
        header-align="center"
        align="center"
        label="门头展示图片">
        <template slot-scope="scope">
          <span v-if="scope.row.imgList.length>2">
            <img v-for="(x, y) in scope.row.imgList.split(',')" :key="y" style="height: 30%;width: 30%" @click="openImg(x)" :src="x"/>
          </span>
          <el-button v-if="isAuth('mall:shops:update')" type="text" size="small" @click="editImage(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
<!--      <el-table-column-->
<!--        prop="userId"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="店铺管理员">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{transUser(scope.row.userId)}}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column-->
<!--        prop="longitude"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="经度">-->
<!--      </el-table-column>-->
<!--      <el-table-column-->
<!--        prop="latitude"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="纬度">-->
<!--      </el-table-column>-->
      <el-table-column
        show-tooltip-when-overflow
        prop="details"
        header-align="center"
        align="center"
        label="详细位置">
      </el-table-column>
      <el-table-column
        show-tooltip-when-overflow
        prop="isOnSale"
        header-align="center"
        align="center"
        label="前台展示">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isOnSale === 0" size="small" type="danger">否</el-tag>
          <el-tag v-else-if="scope.row.isOnSale === 1" size="small" type="success">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        show-tooltip-when-overflow
        prop="telephone"
        header-align="center"
        align="center"
        label="联系电话">
      </el-table-column>
      <el-table-column
        prop="sort"
        header-align="center"
        align="center"
        label="排序">
      </el-table-column>
      <el-table-column
        width="250"
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('mall:shops:info')" type="text" size="small" @click="showDetails(scope.row.id)">查看</el-button>
          <el-button v-if="isAuth('mall:shops:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-if="isAuth('mall:shops:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          <el-button v-if="isAuth('mall:shops:info')" type="text" size="small" @click="clickTag(scope.row.id)">店铺标签</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!--导入-->
    <shops-upload v-if="batchVisible" ref="shopsUpload" @refreshDataList="getDataList"></shops-upload>

    <el-dialog
      title="店铺图片"
      :visible.sync="dialogVisible"
      width="30%">
      <div>
        <el-img v-model="imgUrl1"></el-img>
        <el-img v-model="imgUrl2"></el-img>
        <el-img v-model="imgUrl3"></el-img>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="upLoadImage">确 定</el-button>
      </span>
    </el-dialog>

    <data-import ref="dataImport"></data-import>

    <el-drawer
      :visible.sync="drawer"
      direction="rtl"
      :withHeader="false"
      :modal="false"
      size="60%">
      <div style="padding: 15px;overflow: auto;height: 100vh">
        <associ-tag ref="associTag" :tag-id="tagId"></associ-tag>
      </div>
    </el-drawer>

  </div>
</template>

<script>
import AddOrUpdate from './shops-add-or-update'
import ShopsUpload from './shops-upload'
import AssociTag from './associtag'

export default {
  data() {
    return {
      imgUrl1: '',
      imgUrl2: '',
      imgUrl3: '',
      searchForm: {
        name: '',
        type: ''
      },
      dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListSelections: [],
        dialogVisible: false,
        batchVisible: false,
        addOrUpdateVisible: false,
        drawer: false,
        tagId: ''
      }
    },
    components: {
      AddOrUpdate, ShopsUpload, AssociTag
    },
    activated () {
      this.getDataList()
    },
    methods: {
      clickTag(id){
        this.drawer = true
        this.$nextTick(()=>{
          this.$refs.associTag.$nextTick(() => {
            this.tagId = id
          })
        })
      },
      editImage (obj) {
        this.dialogVisible=true
        this.id = obj.id
        let list = []
        if(obj.imgList && obj.imgList !== ''){
          list = obj.imgList.split(',')
        }
        this.imgUrl1 = list[0]
        this.imgUrl2 = list[1]
        this.imgUrl3 = list[2]
      },
      upLoadImage () {
        let list = []
        let imgs = null
        if(this.imgUrl1){
          list.push(this.imgUrl1)
        }
        if(this.imgUrl2){
          list.push(this.imgUrl2)
        }
        if(this.imgUrl3){
          list.push(this.imgUrl3)
        }
        if(list){
          imgs=list.toString();
        }
        this.$http({
          url: '/mall/shops/updateImage',
          method: 'get',
          params: {
            'imgs': imgs,
            'id': this.id
          }
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500
          })
          this.getDataList()
        })
        this.dialogVisible = false
      },
      // 获取数据列表
      getDataList () {
        this.$http({
          url: '/mall/shops/list',
          method: 'get',
          params: {
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.searchForm.name,
            'type': this.searchForm.type
          }
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.records
            this.totalPage = data.page.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 查看详情
      showDetails (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, true)
        })
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      batchHandle (id) {
        this.batchVisible = true
        this.$nextTick(() => {
          this.$refs.shopsUpload.init(this.cardId)
        })
      },
      // 删除
      deleteHandle (id) {
        let ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[删除]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: '/mall/shops/delete',
            method: 'post',
            data: ids
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.getDataList()
            }
          })
        }).catch(() => {
        })
      }
    }
  }
</script>
